home *** CD-ROM | disk | FTP | other *** search
- Path: news.huji.ac.il!nntp
- Newsgroups: comp.lang.c++
- Subject: Qsort & class - HELP
- Message-ID: <1996Mar2.145458.4120@news.huji.ac.il>
- From: SHALITIN@VMS.HUJI.AC.IL (shalitin)
- Date: 2 Mar 96 14:54:57 GMT
- Distribution: world
- Organization: The Hebrew University
- Nntp-Posting-Host: dial-3-7.slip.huji.ac.il
- X-Newsreader: WinVN 0.92.6+
-
- Hi,
- I am trying to qsort() array of pointers to classes, and I don't succeed.
- I have no idea why this is happenning, but I think the sourse of the problem is in
- the CompareNames() function.
- If anyone can help I would greatly appreaciate it. And let me know what I
- am doing wrong...
- thanks very much..
- Nati
-
- #include <stdlib.h>
- #include <string.h>
-
- #define NUMBER_OF_NAMES 3
-
- class Names
- {
- private:
-
- char Name[10];
-
- public:
-
- void SetName( char *name )
- {
- strcpy( Name, name );
- }
-
- friend void SortNames( Names *names[], int size )
- {
- qsort( names, size, sizeof(names[0]), CompareNames );
- }
-
- friend int CompareNames( const void *name1, const void *name2 )
- {
- return stricmp( ((Names*)name1)->Name, ((Names*)name2)->Name );
- }
- };
-
-
- void main( void )
- {
- Names *names[NUMBER_OF_NAMES];
- char *tab_names[] = { "Michael", "Jeff", "Frank" };
-
-
- for ( int i = 0; i < NUMBER_OF_NAMES; i++ ) {
-
- names[i] = new Names;
- names[i]->SetName( tab_names[i] );
- }
-
- SortNames( names , NUMBER_OF_NAMES );
- }
-